import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";

/**
 * model 汇率定义
 * @date: 2018-7-2


 */
import { getResponse, parseParameters, createPagination } from 'utils/utils';
import { queryIdpValue } from 'cpaas-front/lib/services/api';
import { fetchRateData as _fetchRateData, createRate as _createRate, updateRate as _updateRate, createCrossRate as _createCrossRate } from "../services/rateService";
export var service = {
  init: function init() {
    return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
      return _regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              return _context.abrupt("return", queryIdpValue('SMDM.EXCHANGE_RATE_METHOD'));

            case 1:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    }))();
  }
};
export default {
  namespace: 'rate',
  state: {
    modalVisible: false,
    rateMethodList: [],
    rateList: [],
    pagination: {} // 分页对象

  },
  effects: {
    // 获取初始化数据
    init: /*#__PURE__*/_regeneratorRuntime.mark(function init(_ref, _ref2) {
      var payload, call, put, res, list;
      return _regeneratorRuntime.wrap(function init$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref.payload;
              call = _ref2.call, put = _ref2.put;
              _context2.next = 4;
              return call(service.init, payload);

            case 4:
              res = _context2.sent;
              list = getResponse(res);

              if (!list) {
                _context2.next = 9;
                break;
              }

              _context2.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  rateMethodList: list
                }
              });

            case 9:
            case "end":
              return _context2.stop();
          }
        }
      }, init);
    }),
    // 获取汇率定义信息
    fetchRateData: /*#__PURE__*/_regeneratorRuntime.mark(function fetchRateData(_ref3, _ref4) {
      var payload, call, put, res, list;
      return _regeneratorRuntime.wrap(function fetchRateData$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref3.payload;
              call = _ref4.call, put = _ref4.put;
              _context3.next = 4;
              return call(_fetchRateData, parseParameters(payload));

            case 4:
              res = _context3.sent;
              list = getResponse(res);

              if (!list) {
                _context3.next = 9;
                break;
              }

              _context3.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  rateList: list.content,
                  pagination: createPagination(list)
                }
              });

            case 9:
              return _context3.abrupt("return", list);

            case 10:
            case "end":
              return _context3.stop();
          }
        }
      }, fetchRateData);
    }),
    // 新建汇率定义
    createRate: /*#__PURE__*/_regeneratorRuntime.mark(function createRate(_ref5, _ref6) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function createRate$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref5.payload;
              call = _ref6.call;
              _context4.next = 4;
              return call(_createRate, payload);

            case 4:
              res = _context4.sent;
              return _context4.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context4.stop();
          }
        }
      }, createRate);
    }),
    // 更新汇率定义
    updateRate: /*#__PURE__*/_regeneratorRuntime.mark(function updateRate(_ref7, _ref8) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function updateRate$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref7.payload;
              call = _ref8.call;
              _context5.next = 4;
              return call(_updateRate, payload);

            case 4:
              res = _context5.sent;
              return _context5.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context5.stop();
          }
        }
      }, updateRate);
    }),
    // 生成交叉汇率
    createCrossRate: /*#__PURE__*/_regeneratorRuntime.mark(function createCrossRate(_ref9, _ref10) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function createCrossRate$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              payload = _ref9.payload;
              call = _ref10.call;
              _context6.next = 4;
              return call(_createCrossRate, payload);

            case 4:
              res = _context6.sent;
              return _context6.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context6.stop();
          }
        }
      }, createCrossRate);
    })
  },
  reducers: {
    updateState: function updateState(state, _ref11) {
      var payload = _ref11.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    }
  }
};